/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bookstore.action;

import com.opensymphony.xwork2.*;
import bookstore.iservice.*;
import bookstore.service.*;
import bookstore.dao.*;
import bookstore.model.*;
import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.LOGIN;
import static com.opensymphony.xwork2.Action.SUCCESS;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cjx
 */
public class CartAction extends ActionSupport {
    private String list_index;
    private String count;
    private Book book;
    private String book_id;

    public String getBook_id() {
        return book_id;
    }

    public void setBook_id(String book_id) {
        this.book_id = book_id;
    }
    public String getList_index() {
        return list_index;
    }

    public void setList_index(String list_index) {
        this.list_index = list_index;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }
    public String add()
    {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        List<Book> list = (List<Book>) session.get("bookList");
        book = list.get(Integer.parseInt(list_index));
        List<Good> goodList = (List<Good>) session.get("goodList");
        Good good = new Good();
        if(goodList == null)
        {
            goodList = new ArrayList<Good>();
        }
        good.setBook(book);
        good.setTotal(Integer.parseInt(count)*book.getPrice());
        good.setCount(Integer.parseInt(count));
        goodList.add(good);
        session.put("goodList", goodList);
        return SUCCESS;
    }
    public String delete()
    {
         ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
         List<Good> goodList = (List<Good>) session.get("goodList");
         for(int i=0;i<goodList.size();++i)
         {
             if(Integer.parseInt(book_id) == goodList.get(i).getBook().getId())
                 goodList.remove(i);
         }
         session.put("goodList", goodList);
        return SUCCESS;
    }
    public String execute() {
        

        return SUCCESS;
    }

}
